﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using System.IO;
using System.Xml;
using BPM.Base.DataTypeLib;

using BPM.Base.Tools;
using BPM.Foundation.WFService;
using BPM.Foundation.WFDB;


namespace BPM.Base.WFLib
{
    [System.Drawing.ToolboxBitmap(typeof(流程管理), "pic.manage.png")]
	public partial class 流程管理: Activity
	{
        public static DependencyProperty 数据表单Property = DependencyProperty.Register("数据表单", typeof(string), typeof(流程管理), new PropertyMetadata(""));

        [DescriptionAttribute("")]
        [CategoryAttribute("")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public string 数据表单
        {
            get
            {
                return ((string)(base.GetValue(流程管理.数据表单Property)));
            }
            set
            {
                base.SetValue(流程管理.数据表单Property, value);
            }
        }

        public static DependencyProperty 管理类型Property = DependencyProperty.Register("管理类型", typeof(string), typeof(流程管理), new PropertyMetadata(""));

        [DescriptionAttribute("")]
        [CategoryAttribute("")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public string 管理类型
        {
            get
            {
                return ((string)(base.GetValue(流程管理.管理类型Property)));
            }
            set
            {
                base.SetValue(流程管理.管理类型Property, value);
            }
        }

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            WorkflowService service = executionContext.GetService(typeof(WorkflowService)) as WorkflowService;
            
            if(service!=null)
            {
                SYS_Log myLog = new SYS_Log();
                myLog.instanceId = this.WorkflowInstanceId;
                myLog.logContent = string.Format("流程管理:{0}", DateTime.Now);
                myLog.logType = "Activity";
                service.insertSYSLog("",myLog);
            }
            //判断数据表单是否为空
            if (!string.IsNullOrEmpty(this.数据表单))
            {
                SYS_Log myLog = new SYS_Log();
                myLog.instanceId = this.WorkflowInstanceId;
                myLog.logContent = string.Format("数据表单不为空:{0}", DateTime.Now);
                myLog.logType = "Activity";
                service.insertSYSLog("", myLog);
                StringReader stream = null;
                XmlTextReader reader = null;


                DataForm myDataForm = new DataForm();

                //把数据表单字符串读取到StringReader
                stream = new StringReader(this.数据表单);

                //从stream装载到XmlTextReader

                reader = new XmlTextReader(stream);


                //把xmlTextReader 读到 dataSet
                myDataForm.ReadXml(reader);

                //判断转化后的dataset是否为空
                if (myDataForm != null)
                {

                    myLog = new SYS_Log();
                    myLog.instanceId = this.WorkflowInstanceId;
                    myLog.logContent = string.Format("DataSet不为空:{0}", DateTime.Now);
                    myLog.logType = "Activity";
                    service.insertSYSLog("", myLog);

                    switch (this.管理类型)
                    {

                        case "强行跳转":

                            #region "强行跳转"
                            if (myDataForm.gotoStateTab != null)
                            {

                                myLog = new SYS_Log();
                                myLog.instanceId = this.WorkflowInstanceId;
                                myLog.logContent = string.Format("强行跳转:{0}", DateTime.Now);
                                myLog.logType = "Activity";
                                service.insertSYSLog("", myLog);
                                DataForm.gotoStateTabRow myGotoState = myDataForm.gotoStateTab.FirstOrDefault();

                                if (myGotoState == null)
                                {
                                    string msg = string.Format("类名称:{0} 方法名:{1}  强行跳转所需的数据表单为null, 错误发生的时间:{2}", this.Name, "Excute", DateTime.Now);
                                    MyLog.writeError(msg);
                                    return ActivityExecutionStatus.Closed;
                                }
                                if (myGotoState.instanceID == Guid.Empty)
                                {
                                    myGotoState.instanceID = this.WorkflowInstanceId;

                                }

                                int? result = service.gotoState("",
                                    myGotoState.instanceID,
                                    myGotoState.stateName,
                                    myGotoState.operateDepartmentNo,
                                    myGotoState.operateDutyNo,
                                    myGotoState.operateUserNo,
                                    myGotoState.description

                                  );
                               
                            }
                            #endregion
                            break;
                        case "延期":
                            #region "延期"
                            if (myDataForm.statePostponeTab != null)
                            {
                                DataForm.statePostponeTabRow myState = myDataForm.statePostponeTab.FirstOrDefault();
                                if (myState == null)
                                {
                                    string msg = string.Format("类名称:{0} 方法名:{1}  延期所需的数据表单为null, 错误发生的时间:{2}", this.Name, "Excute", DateTime.Now);
                                    MyLog.writeError(msg);
                                    return ActivityExecutionStatus.Closed;
                                }
                                int? result = service.statePostpone("",
                                   myState.instanceID,
                                   myState.stateInstanceID,

                                   myState.value,
                                   myState.operateUserNo,
                                   myState.operateDepartmentNo,
                                   myState.operateDutyNo,
                                   myState.description
                                   );
                              
                            }

                            #endregion
                            break;

                        case "接件":
                        case "退回到组":
                        case "分配":
                        case "拒绝分配":
                        case "授权他人":
                        case "取回授权":
                        case "冻结":
                        #region "冻结"
                            if (myDataForm.statePostponeTab != null)
                            {
                                DataForm.statePostponeTabRow myState = myDataForm.statePostponeTab.FirstOrDefault();
                                if(myState==null)
                                {
                                    string msg = string.Format("类名称:{0} 方法名:{1}  冻结所需的数据表单为null, 错误发生的时间:{2}",this.Name,"Excute", DateTime.Now);
                                    MyLog.writeError(msg);
                                    return ActivityExecutionStatus.Closed;
                                }
                                int? result = service.processFreeze
                                    ("",
                                    myState.instanceID,
                                    1,
                                    myState.operateDepartmentNo,
                                    myState.operateDutyNo,
                                    myState.operateUserNo,
                                    myState.description
                                   );

                               
                            }

                        #endregion
                            break;

                        case "解冻":
                            #region"解冻"
                            if (myDataForm.statePostponeTab != null)
                            {
                                DataForm.statePostponeTabRow myState = myDataForm.statePostponeTab.FirstOrDefault();
                                if (myState == null)
                                {
                                    string msg = string.Format("类名称:{0} 方法名:{1}  解冻所需的数据表单为null, 错误发生的时间:{2}", this.Name, "Excute", DateTime.Now);
                                    MyLog.writeError(msg);
                                    return ActivityExecutionStatus.Closed;
                                }
                                int? result = service.processFreeze
                                    ("",
                                    myState.instanceID,
                                    0,
                                    myState.operateDepartmentNo,
                                    myState.operateDutyNo,
                                    myState.operateUserNo,
                                    myState.description
                                   );
                               
                            }
                            #endregion



                            break;



                    }
                }
            }
            return base.Execute(executionContext);
        }
	}
}
